home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / MibManagerBER.java < prev    next >
Text File  |  1997-06-09  |  4KB  |  130 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. $Id: MibManagerBER.java,v 1.1 1997/05/21 13:25:18 alex Exp $
  6.  
  7. This software maybe be free distributed, any any form, without fee, 
  8. but may not be modified in any way without express permission of 
  9. the directors of Alex Kowalenko Associates Pty Ltd. 
  10.  
  11. Alex Kowalenko Associates Pty Ltd makes no representations or
  12. warranties about the suitabililty of the software, not even the
  13. implied warranty of merchantability or fitness for any particular
  14. purpose.    
  15. */
  16.  
  17. package aka.snmp;
  18.  
  19. import java.util.*;
  20. import java.io.*;
  21.  
  22. /**
  23.  * Extends MibManagerPaser with methods to store and retrieve the
  24.  * managed object definitions as an ASN BER encoded file.
  25.  * @see SnmpObject
  26.  * @version     $Id: MibManagerBER.java,v 1.1 1997/05/21 13:25:18 alex Exp $
  27.  * @author      Alex Kowalenko
  28.  */
  29.  
  30. public class MibManagerBER extends MibManagerParser {
  31.  
  32. /**
  33.  * Constructor
  34.  */
  35.  
  36.   public MibManagerBER() {
  37.       super();
  38.   };
  39.   
  40.  
  41. /**
  42.  * Store the Managed Object definitions to a file, using BER encoding
  43.  * (This implements serializing of the MIB in Java 1.0).
  44.  * Use MibManagerSerialize which uses
  45.  * Java 1.1 serialization.  Though these routines are faster and more 
  46.  * compact than Java 1.1 serialization
  47.  * @see MibManagerSerialize
  48.  */
  49.  
  50.   public void BERSerializeToFile(String filename) 
  51.       throws FileNotFoundException, IOException {
  52.       
  53.       BufferedOutputStream output 
  54.           = new BufferedOutputStream(new FileOutputStream(filename));
  55.       for(Enumeration e = _tableName.elements(); e.hasMoreElements(); ) {
  56.           ByteBuffer buf = ((SnmpObject)e.nextElement()).BERSerialize();
  57.           output.write(buf.array(), 0, buf.size()); 
  58.       };
  59.       output.close();
  60.       return;
  61.   };
  62.  
  63. /**
  64.  * Read the Managed object defintion from a BER encoded file
  65.  * Use MibManagerSerialize which uses
  66.  * Java 1.1 serialization. Though is faster and more compact than Java
  67.  * 1.1 serialization.
  68.  * @see MibManagerSerialize
  69.  */
  70.  
  71.   public void BERSerializeFromFile(String filename) 
  72.       throws FileNotFoundException, IOException, SnmpPDUException{
  73.         
  74.       FileInputStream input = new FileInputStream(filename);
  75.       ByteBuffer buffer = new ByteBuffer();
  76.       byte b[] = new byte[2048];
  77.       int count = input.read(b);
  78.       while(count > 0) {
  79.           buffer.append(b, count);
  80.           count = input.read(b);
  81.       };
  82.       while(buffer.size() > 0) {
  83.           SnmpObject obj = new SnmpObject(buffer);
  84.           _tableName.put(obj.name(), obj);
  85.           _tableID.put(obj.id(), obj);
  86.       };
  87.   };
  88.  
  89.   public static void main(String args[]) {
  90.       PrintStream out = System.out;
  91.  
  92.       out.println("Creating SNMP MIB MANAGER");
  93.       MibManagerBER mibs = new MibManagerBER();
  94.  
  95.       out.println("Reading SNMP mib");
  96.       try {
  97.       mibs.addFromMibDefFile("mib1.txt");
  98.       } catch(FileNotFoundException e) {
  99.       out.println("File not found");
  100.       return;
  101.       } catch(Exception e) {
  102.       out.println("Exception");
  103.       e.printStackTrace();
  104.       mibs.dump();
  105.       };
  106.  
  107.       //  mibs.dump();
  108.       //  mibs.dump2();
  109.   
  110.       try {
  111.       out.println("Write to BER file");
  112.       mibs.BERSerializeToFile("mibs.ber");
  113.       } catch(Exception e) {
  114.       out.println("Excepton with BERSerializeToFile : " + e.getMessage());
  115.       e.printStackTrace();
  116.       };
  117.   
  118.       MibManagerBER mib2 = new MibManagerBER();
  119.       try {
  120.       out.println("Read from BER file");
  121.       mib2.BERSerializeFromFile("mibs.ber");
  122.       } catch(Exception e) {
  123.       out.println("Excepton with BERSerializeToFile : " + e.getMessage());
  124.       e.printStackTrace();
  125.       };
  126.       mib2.dump();
  127.   }
  128.  
  129. };
  130.